package thread.lockAndCondition;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Description: lock demo
 * Created by E440 on 2017/4/24.
 */
public class LockDemo {
    public static void main(String[] args) {
        final Lock lock=new ReentrantLock();
        final  String[] names={"ningshaolin","daijingjia","ningqianqian"};
        for (int i = 0; i < 3; i++) {
            final  int seq=i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    lock.lock();
                    print(names[seq]);
                    lock.unlock();
                }
            }).start();
        }
    }

    public static void print(String name){
        for (int i = 0; i < name.length(); i++) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.print(name.charAt(i));
        }
        System.out.println();
    }
}
